package com.sap.listen;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author luoh6
 */
@Component
public class RabbitListen {

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "myQueue", durable = "true"),  // 声明队列
            exchange = @Exchange(name = "rabbit", type = ExchangeTypes.DIRECT),  // 声明交换机
            key = "test"  // 路由key
    ))
    public void getMessage(String message) {
        System.out.println("接收到消息：" + message);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "myQueue"), exchange = @Exchange(name = "rabbit", type = ""), key = "test"))
    public void getMessage2(String message) {
        System.out.println("接收到消息：" + message);
    }

    // 使用 @RabbitListener 声明延迟交换机和队列
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "delayed.queue", durable = "true"  // 持久化队列
    ), exchange = @Exchange(value = "delayed.exchange", type = "x-delayed-message",  // 指定为延迟交换机
            durable = "true", arguments = @Argument(name = "x-delayed-type", value = "direct"  // 底层路由方式（支持 direct/topic/fanout）
    )), key = "delayed.routing.key"  // 路由键
    ))
    public void handleDelayedMessage(String message) {
        System.out.println("收到延迟消息: " + message);
    }
}
